package com.fin.quant.rpc.bean;

import com.orbitz.consul.Consul;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class GrpcClientChannelFactory {

    private final Consul consul;

    public GrpcClientChannelFactory(Consul consul) {
        this.consul = consul;
    }

    public ManagedChannel getChannel(String serviceName) {
        log.info("GrpcClientChannelFactory getChannel. serviceName={} 实际使用了PythonGrpcServer", serviceName);
        //临时替换
        serviceName = "PythonGrpcServer";

        ManagedChannel channel = ManagedChannelBuilder
                .forTarget("consul://" + serviceName)  // 通过 Consul NameResolver
                .defaultLoadBalancingPolicy("round_robin")
                .usePlaintext()
                .build();

        return channel;
    }
}
